home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / proxyfind.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  2.7 KB  |  79 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. """proxyfind.py.  Get proxy info from windows."""
  19.  
  20. from ctypes import Structure, byref, windll
  21. from ctypes.wintypes import LPWSTR, BOOL
  22. import logging
  23. import re
  24.  
  25. class WINHTTP_CURRENT_USER_IE_PROXY_CONFIG(Structure):
  26.     _fields_ = [
  27.         ('fAutoDetect', BOOL),
  28.         ('lpszAutoConfigUrl', LPWSTR),
  29.         ('lpszProxy', LPWSTR),
  30.         ('lpszProxyBypass', LPWSTR),
  31.     ]
  32.  
  33. class ProxyInfo:
  34.     def __init__(self):
  35.         self.active = False
  36.         self.host = self.port = None
  37.         self.ignore_hosts = []
  38.  
  39. def get_proxy_info():
  40.     proxy_info = ProxyInfo()
  41.     ie_proxy_info = WINHTTP_CURRENT_USER_IE_PROXY_CONFIG()
  42.     rv = windll.winhttp.WinHttpGetIEProxyConfigForCurrentUser(
  43.             byref(ie_proxy_info))
  44.  
  45.     if not rv or ie_proxy_info.lpszProxy is None:
  46.         return proxy_info
  47.     proxy_info.host, proxy_info.port = \
  48.             parse_host_and_port(ie_proxy_info.lpszProxy)
  49.     if ie_proxy_info.lpszProxyBypass is not None:
  50.         proxy_info.ignore_hosts = re.split("[;\s]*", 
  51.                 ie_proxy_info.lpszProxyBypass)
  52.     return proxy_info
  53.  
  54. def parse_host_and_port(windows_proxy_string):
  55.     for proxy in re.split("[;\s]*", windows_proxy_string):
  56.         original_string = proxy
  57.         if proxy.startswith("http="):
  58.             proxy = proxy[len('http='):]
  59.         elif '=' in proxy or proxy == '':
  60.             continue
  61.         if '://' in proxy:
  62.             if proxy.startswith("http://"):
  63.                 proxy = proxy[len("http://"):]
  64.             else:
  65.                 logging.warn("unsupported proxy scheme: %s" % original_string)
  66.                 continue
  67.         if ':' in proxy:
  68.             proxy, port = proxy.split(":")
  69.             try:
  70.                 port = int(port)
  71.             except ValueError:
  72.                 logging.warn("bad proxy port: %s" % original_string)
  73.                 continue
  74.         else:
  75.             port = 80
  76.         return proxy, port
  77.     logging.warn("couldn't find proxy: %s" % windows_proxy_string)
  78.     return None, None
  79.